package tuiLayer;

import controlLayer.DVDCtr;
import modelLayer.DVD;
import modelLayer.Copy;

import java.util.Scanner;
/**
 * Write a description of class DVDUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class DVDUI
{
    // instance variables - replace the example below with your own
    private DVDCtr dvdCtr;

    /**
     * Constructor for objects of class DVDUI
     */
    public DVDUI()
    {

        dvdCtr = new DVDCtr();
    }
    // Create a DVD
    public void createDVD(String barcode, String title, String artist, String publicationDate, double price)
    {
        dvdCtr.createDVD(barcode, title, artist, publicationDate, price);
    }

    // Find a DVD
    public DVD findDVD(String barcode)
    {
        return dvdCtr.findDVD(barcode);
    }

    // Update a DVD
    public void updateDVD(DVD dvd, String barcode, String title, String artist, String publicationDate, double price)
    {
        dvdCtr.updateDVD(dvd,barcode,title,artist,publicationDate,price);
    }

    // Delete a DVD
    public void deleteDVD(DVD dvd)
    {
        dvdCtr.deleteDVD(dvd);
    }

    // Create a copy
    public void createCopy(DVD dvd,String serialNumber, String purchaseDate)
    {
        dvdCtr.createCopy(dvd,serialNumber,purchaseDate);
    }

    // Find a copy
    public Copy findCopy(DVD dvd, String serialNumber)
    {
        return dvdCtr.findCopy(dvd,serialNumber);
    }

    // Update a copy
    public void updateCopy(DVD dvd, Copy copy, String serialNumber, String purchaseDate) 
    {
        dvdCtr.updateCopy(dvd,copy,serialNumber,purchaseDate);
    }

    // Delete a copy
    public void deleteCopy(DVD dvd, Copy copy)
    {
        dvdCtr.deleteCopy(dvd,copy);
    }

    private int getChoice()
    {
        Scanner keyboard = new Scanner(System.in);
        int choice = keyboard.nextInt();

        return choice;
    }

    // DVDs menu loop
    public void menuStart() 
    {
        boolean exit = false;
        String print = null;
        while(!exit) 
        {
            writeDvdsMenu(print);
            int choice = getChoice();

            if(choice == 1) 
            {
                print = null;
                dvdsMenuStart();
            }
            else if(choice == 2)
            {
                print = copiesMenuStart();
            }
            else if(choice == 3)
            {
                print = null;
                exit = true;
            }

        }
    }

    // Write DVDs menu
    public void writeDvdsMenu(String print) 
    {
        System.out.println("\f *** DVDs ***");
        System.out.println(" (1)Manage DVDs");
        System.out.println(" (2)Manage Copies");
        System.out.println(" (3)Back");
        if(print != null) 
        {
            System.out.println("\n " + print);

        }
        System.out.print("\n Make your choice: ");
    }
     
    
    
    
    
    
    // START MANAGE DVDs MENU
    // Manage DVDs menu loop
    public void dvdsMenuStart() 
    {
        boolean exit = false;
        String print = null;
        while(!exit) 
        {
            writeManageDvdsMenu(print);
            int choice = getChoice();

            if(choice == 1) 
            {
                
                print = dvdsAddStart();
            }
            else if(choice == 2)
            {
                print = dvdsFindStart();
            }
            else if(choice == 3)
            {
                
                print = dvdsUpdateStart();
            }
            else if(choice == 4) 
            {
                print = dvdsRemoveStart();
            }
            else if(choice == 5) 
            {
                print = null;
                exit = true;
            }

        }
    }
    // Write Manage DVDs menu
    public void writeManageDvdsMenu(String print) 
    {
        System.out.println("\f *** Manage DVDs ***");
        System.out.println(" (1)Add");
        System.out.println(" (2)Find");
        System.out.println(" (3)Update");
        System.out.println(" (4)Remove");
        System.out.println(" (5)Back");
        if(print != null) 
        {
            System.out.println("\n " + print);

        }
        System.out.print("\n Make your choice: ");
    }

    // Start "Add a DVD"
    public String dvdsAddStart() 
    {
        System.out.println("\nEnter details for the new DVD: ");
        System.out.println("Format: <barcode> <title> <artist> <publication date> <price>");
        
        String print = null;
        
        
        
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        String[] inputWords = input.split(" ");
        if(inputWords.length == 5 && isDouble(inputWords[4])) { //Checking the format
            String barcode = inputWords[0];
            String title = inputWords[1];
            String artist = inputWords[2];
            String publicationDate = inputWords[3];
            double price = Double.parseDouble(inputWords[4]);
            createDVD(barcode,title,artist,publicationDate,price);
            
            print = "DVD #" + barcode + " has been created.";
       }
       else 
       {
           print = "ERROR: Bad input.";
       }
       
       return print;

    }
    
    
    // Check if string is parseable to double
    private boolean isDouble(String string) 
    {
    try
            {
               Double.parseDouble(string);
               return true;
            }
            catch(NumberFormatException e)
            {
               return false;
            }
    }
    
    // Start "Find a DVD"
    public String dvdsFindStart() 
    {
        String print = null;

        System.out.print("\nEnter barcode: ");

        Scanner sc = new Scanner(System.in);
        String barcode = sc.next();

        DVD foundDVD = findDVD(barcode);
        if(foundDVD != null) {
            print = " # Found DVD: #" + "\n" 
                    + "  Barcode: " + foundDVD.getBarcode() + "\n"
                    + "  Title: " + foundDVD.getTitle() + "\n"
                    + "  Artist: " + foundDVD.getArtist() + "\n"
                    + "  Publication date: " + foundDVD.getPublicationDate() + "\n"
                    + "  Price: " + foundDVD.getPrice() + "\n";
                    
            
            /*System.out.println(" # Found DVD: #");
            System.out.println("  Barcode: " + foundDVD.getBarcode());
            System.out.println("  Title: " + foundDVD.getTitle());
            System.out.println("  Artist: " + foundDVD.getArtist());
            System.out.println("  Publication date: " + foundDVD.getPublicationDate());
            System.out.println("  Price " + foundDVD.getPrice());*/
        }
        else 
        {
            print = "ERROR: DVD not found.";
        }
        return print;

    }
    
    private String getDVDinfo(DVD dvd) 
    {
        String string = "  Barcode: " + dvd.getBarcode() + "\n"
                    + "  Title: " + dvd.getTitle() + "\n"
                    + "  Artist: " + dvd.getArtist() + "\n"
                    + "  Publication date: " + dvd.getPublicationDate() + "\n"
                    + "  Price: " + dvd.getPrice() + "\n";
        return string;
    }
    
    // Start "Update a DVD"
    public String dvdsUpdateStart()
    {
        String print = null;
        
        System.out.print("\nEnter barcode: ");

        Scanner sc = new Scanner(System.in);
        String barcode = sc.next();

        DVD foundDVD = findDVD(barcode);
        
        if(foundDVD != null) 
        {
            System.out.println("\nEnter the new details for DVD #" + barcode + " :");
            System.out.println("Format: <barcode> <title> <artist> <publication date> <price>");
            
            Scanner newSc = new Scanner(System.in);
            String input = newSc.nextLine();
            String[] inputWords = input.split(" ");
            if(inputWords.length == 5 && isDouble(inputWords[4])) { //Checking the format
                String newBarcode = inputWords[0];
                String newTitle = inputWords[1];
                String newArtist = inputWords[2];
                String newPublicationDate = inputWords[3];
                double newPrice = Double.parseDouble(inputWords[4]);
                updateDVD(foundDVD,newBarcode,newTitle,newArtist,newPublicationDate,newPrice);
                
                print = "DVD #" + newBarcode + " has been updated to:\n" + getDVDinfo(foundDVD);
                
           }
           else 
           {
               print = "ERROR: Bad input.";
           }
        }
        else
        {
            print = "ERROR: DVD not found.";
        }
        return print;
    }
    
    // Start "Remove a DVD"
    public String dvdsRemoveStart() 
    {
        String print = null;
        
        System.out.print("\nEnter barcode: ");

        Scanner sc = new Scanner(System.in);
        String barcode = sc.next();

        DVD foundDVD = findDVD(barcode);
        
        if(foundDVD != null) 
        {
            deleteDVD(foundDVD);
            print = "DVD #" + barcode + " has been deleted.";
        }
        else 
        {
            print = "ERROR: DVD not found.";
        }
        return print;
    }
    
    // END MANAGE DVDs MENU  
    
    
    
    
    
    
    // START MANAGE Copies MENU
    // Manage Copies menu loop
    public String copiesMenuStart() 
    {
        String print = null;
        
        boolean exit = false;
        System.out.print(" Choose a DVD by barcode:");

        Scanner sc = new Scanner(System.in);
        String barcode = sc.next();

        DVD foundDVD = dvdCtr.findDVD(barcode); 
        if(foundDVD != null) 
        {

            while(!exit) 
            {
                writeManageCopiesMenu(foundDVD,print);
                int choice = getChoice();

                if(choice == 1) 
                {
                    print = copiesAddStart(foundDVD);
                }
                else if(choice == 2)
                {
                    print = copiesFindStart(foundDVD);
                }
                else if(choice == 3)
                {
                    print = copiesUpdateStart(foundDVD);
                }
                else if(choice == 4) 
                {
                    print = copiesRemoveStart(foundDVD);
                }
                else if(choice == 5) 
                {
                    exit = true;
                }

            }
            return null;
        }
        else 
        {                               
            return "ERROR: DVD not found.";
        }
    }

    // Write Manage Copies menu
    public void writeManageCopiesMenu(DVD dvd, String print) 
    {
        System.out.println("\f *** Manage Copies - DVD #" + dvd.getBarcode() + " ***");
        System.out.println(" (1)Add");
        System.out.println(" (2)Find");
        System.out.println(" (3)Update");
        System.out.println(" (4)Remove");
        System.out.println(" (5)Back");
        if(print != null) 
        {
            System.out.println("\n " + print);

        }
        System.out.print("\n Make your choice: ");
    }
    
    
    //Start "Add a copy"
    public String copiesAddStart(DVD dvd) 
    {
        String print = null;
        
        System.out.println("\nEnter details for the new copy: ");
        System.out.println("Format: <serial number> <purchase date>");
        
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        String[] inputWords = input.split(" ");
        if(inputWords.length == 2) 
        {
            String serialNumber = inputWords [0];
            String purchaseDate = inputWords [1];
            createCopy(dvd,serialNumber,purchaseDate);
            
            print = "Copy #" + serialNumber + " of DVD #" + dvd.getBarcode() + " has been created.";
        }
        else 
        {
            print = "ERROR: Bad input";
        }
        return print;
    }
    
    //Start "Find a copy"
    public String copiesFindStart(DVD dvd) 
    {
        String print = null;
        
        System.out.print("\nEnter serial number:");
        
        Scanner sc = new Scanner(System.in);
        String serialNumber = sc.next();
        
        Copy foundCopy = findCopy(dvd, serialNumber);
        if(foundCopy != null) 
        {
            print = " # Found Copy: #" + "\n"
                    + "  Serial number: " + foundCopy.getSerialNumber() + "\n"
                    + "  Purchase date: " + foundCopy.getPurchaseDate() + "\n";
        }
        else 
        {
            print = "ERROR: Copy not found.";
        }
        return print;        
    }
    
    // Start "Update a Copy"
    public String copiesUpdateStart(DVD dvd)
    {
        String print = null;
        
        System.out.print("\nEnter serial number: ");

        Scanner sc = new Scanner(System.in);
        String serialNumber = sc.next();

        Copy foundCopy = findCopy(dvd,serialNumber);
        
        if(foundCopy != null) 
        {
            System.out.println("\nEnter the new details for Copy #" + serialNumber + " :");
            System.out.println("Format: <serial number> <purchase date>");
            
            Scanner newSc = new Scanner(System.in);
            String input = newSc.nextLine();
            String[] inputWords = input.split(" ");
            if(inputWords.length == 2) { //Checking the format
                String newSerialNumber = inputWords[0];
                String newPurchaseDate = inputWords[1];
                updateCopy(dvd,foundCopy,newSerialNumber,newPurchaseDate);
                
                print = "Copy #" + newSerialNumber + " has been updated to:\n" + getCopyInfo(foundCopy);
                
           }
           else 
           {
               print = "ERROR: Bad input.";
           }
        }
        else
        {
            print = "ERROR: Copy not found.";
        }
        return print;
    }
    
    //Print copy info
    private String getCopyInfo(Copy copy) 
    {
        String string = "  Serial number: " + copy.getSerialNumber() + "\n"
                    + "  Purchase date: " + copy.getPurchaseDate() + "\n";
        return string;
    }
    
    // Start "Remove a Copy"
    public String copiesRemoveStart(DVD dvd) 
    {
        String print = null;
        
        System.out.print("\nEnter serial number: ");

        Scanner sc = new Scanner(System.in);
        String serialNumber = sc.next();

        Copy foundCopy = findCopy(dvd,serialNumber);
        
        if(foundCopy != null) 
        {
            deleteCopy(dvd,foundCopy);
            print = "Copy #" + serialNumber + " of DVD #" + dvd.getBarcode()+ " has been deleted.";
        }
        else 
        {
            print = "ERROR: Copy not found.";
        }
        return print;
    }
    
}








